// JavaScript Document
var xmlHttp;
var xmlHttpEmail;
var xmlHttpUser;
var xmlHttpMaXN;
var xmlHttpOldPass;

var iuser = 0;
var iemail = 0;
var ima = 0;
var ipass = 0;
var i2pass = 0;
var ioldpass = 0;
var icheck = 0;

function getAllField()
{	
	document.getElementById('loadLinhVuc').style.display = "block";
	document.getElementById('bntDangKy').disabled = true;
	
	xmlHttp = getXmlHttpObject();
	if(xmlHttp == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	
	var url = "../common/kt_dangky.php";
	url += "?LinhVuc=" + 1;
	url += "&t=" + new Date().getTime();

	xmlHttp.onreadystatechange = handleResponseLinhVuc;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


function handleResponseLinhVuc()
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{		
		document.getElementById('loadLinhVuc').style.display = "none";
		var kq = xmlHttp.responseText;
		document.getElementById('tdLinhVuc').innerHTML = kq;
	}
}

function handleResponseLinhVuc()
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{		
		document.getElementById('loadLinhVuc').src = "";
		var kq = xmlHttp.responseText;
		document.getElementById('tdLinhVuc').innerHTML = kq;
	}
}

//kiem tra email
	
function KTEmail(mail)
{
	var str = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/; 
	
	if(mail.length == 0)
	{
		baoloi("lbEmail"," * Vui lòng nhập email!");
		iemail = 0;
		return;
	}
	if(!mail.match(str))
	{ 
		baoloi("lbEmail","* Email không hợp lệ");
		iemail = 0;
		return;
	} 
		
	xmlHttpEmail = getXmlHttpObject();
	if(xmlHttpEmail == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	
	var url = "../common/kt_dangky.php";
	url += "?mail=" + mail;
	url += "&t=" + new Date().getTime();

	xmlHttpEmail.onreadystatechange = handleResponseEmail;
	xmlHttpEmail.open("GET",url,true);
	xmlHttpEmail.send(null);	
}

function handleResponseEmail()
{
	document.getElementById('lbEmail').innerHTML = "";
	if(xmlHttpEmail.readyState == 4 && xmlHttpEmail.status == 200)
	{
		var kq = xmlHttpEmail.responseText;
		if(kq == 1)
		{
			iemail = 1;
		}
		else
		{
			document.getElementById('lbEmail').innerHTML = kq;
			iemail = 0;
		}
	}
}

function KTPass1_Pass2()
{
	var pass1 = document.getElementsByName("txtPassword1")[0];
	var pass2 = document.getElementsByName("txtPassword2")[0];
		
	if(pass1.value != pass2.value)
	{
		baoloi("lbPassword2","* Mật khẩu phải giống nhau");
		i2pass = 0;
		return;
	}
	baoloi("lbPassword2","");
	i2pass = 1;

}

function KTPass(pass)
{
	if(pass.length == 0)
	{
		baoloi("lbPassword1"," * Vui lòng nhập mật khẩu!");
		ipass = 0;
		return;
	}
	else if(pass.length < 6)
	{
		baoloi("lbPassword1","Mật khẩu phải trên 5 kí tự.");
		ipass = 0;
		return;
	}
	baoloi("lbPassword1","");
	ipass = 1;
}

function baoloi(name,Loi)
{
	document.getElementById(name).innerHTML = "<font color='red'>" + Loi + "</font>";
}

function KTUsername(username)
{
	if(username == '')
	{
		baoloi("lbUsername"," * Vui lòng nhập tên đăng nhập!");
		iuser = 0;
		return;
	}
	if(username.indexOf(' ') >= 0)
	{
		baoloi("lbUsername"," * Tên đăng nhập không được chứa khoảng trắng!");
		iuser = 0;
		return;
	}
	
	xmlHttpUser = getXmlHttpObject();
	if(xmlHttpUser == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	
	var url = "../common/kt_dangky.php";
	url += "?username=" + username;
	url += "&t=" + new Date().getTime();
	
	xmlHttpUser.onreadystatechange = handleResponseUsername;
	xmlHttpUser.open("GET",url,true);
	xmlHttpUser.send(null);
}

function handleResponseUsername()
{
	if(xmlHttpUser.readyState == 4 && xmlHttpUser.status == 200)
	{
		document.getElementById('lbUsername').innerHTML = "";
		var kq = xmlHttpUser.responseText;
		if(kq == 1)
		{
			iuser = 1;
		}
		else
		{
			document.getElementById('lbUsername').innerHTML = kq;
			iuser = 0;
		}
	}
}

function KTMaXN(ma)
{
	if(ma == '')
	{
		baoloi("lbMaXacNhan"," * Vui lòng nhập mã xác nhận!");
		ima = 0;
		return;
	}
	
	xmlHttpMaXN = getXmlHttpObject();
	if(xmlHttpMaXN == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	
	var url = "../common/kt_dangky.php";
	url += "?maxn=" + ma;
	url += "&t=" + new Date().getTime();
	xmlHttpMaXN.onreadystatechange = handleResponseMaXN;
	xmlHttpMaXN.open("GET",url,true);
	xmlHttpMaXN.send(null);
}

function handleResponseMaXN()
{
	if(xmlHttpMaXN.readyState == 4 && xmlHttpMaXN.status == 200)
	{
		document.getElementById('lbMaXacNhan').innerHTML = "";
		
		var kq = xmlHttpMaXN.responseText;
		if(kq == 1)
		{
			ima = 1;
		}
		else
		{
			document.getElementById('lbMaXacNhan').innerHTML = kq;
			ima = 0;
		}
	}
}

function handleResponseRegister()
{
	if(xmlHttp.readyState == 4 && xmlHttp.status == 200)
	{
		document.getElementById('loadWait').style.display = "none";
		var kq = xmlHttp.responseText;
		document.getElementById('divDangKy').innerHTML = kq;
	}
}

function getXmlHttpObject()
{
	if(window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}
	if(window.ActiveXObject)
	{
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}


function changeCode()
{
	xmlHttpMaXN = getXmlHttpObject();
	if(xmlHttpMaXN == null)
	{
		alert("Your browser does not support XML HTTP Request!!!");
		return;
	}
	
	var url = "../common/captcha/random_image.php";
	xmlHttpMaXN.onreadystatechange = handleResponseDoiMa;
	xmlHttpMaXN.open("GET",url,true);
	xmlHttpMaXN.send(null);
}

function handleResponseDoiMa()
{
	if(xmlHttpMaXN.readyState == 4 && xmlHttpMaXN.status == 200)
	{
		var kq = xmlHttpMaXN.responseText;
		document.getElementById('imgCaptcha').src = "../common/captcha/code"+kq+".jpg";
	}
}


function checkAllRight()
{	
	var user = document.getElementsByName('txtUsername')[0].value;
	var mail = document.getElementsByName('txtEmail')[0].value;
	var ma = document.getElementsByName('txtMaXacNhan')[0].value;
	var pass1 = document.getElementsByName('txtPassword1')[0].value;
	
	KTUsername(user);
	KTEmail(mail);
	KTMaXN(ma);
	KTPass(pass1);
	KTPass1_Pass2();
	
	if(iuser == 1 && iemail == 1 && ima == 1 && ipass == 1 && i2pass == 1)
	{
		return 1;
	}
	document.getElementById('loadWait').style.display = "none";
	return 0;
}

function checkAgree()
{
	var node = document.getElementById('idCheckAgree');
	if(node.checked == true)
	{
		document.getElementById('bntDangKy').disabled = false;
	}
	else
	{
		document.getElementById('bntDangKy').disabled = true;
	}
}

function Run()
{
	if(checkAllRight() == 1)	
	{
		document.frmDangKy.submit();
	}
}

function KTSubmit()
{
	checkAllRight();
	document.getElementById('loadWait').style.display = "block";
	setTimeout('Run()',3000);
}